/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.ui;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.lang.ref.WeakReference;
import org.openide.util.datatransfer.ExTransferable;

/**
 *
 * @author bennyl
 */
public class DNDs {

    private static WeakReference dragging;

    /**
     *
     * @param t
     * @param df
     * @param toPack
     * @return
     */
    public static Transferable with(Transferable t, DataFlavor df, final Object toPack) {
        dragging = new WeakReference(toPack);
        ExTransferable result = ExTransferable.create(t);
        result.put(new ExTransferable.Single(df) {
            @Override
            protected Object getData() throws IOException, UnsupportedFlavorException {
                return "Call DNDs.getDrag to extract";
            }
        });

        return result;
    }

    public static void setDrag(Object drag) {
        dragging = new WeakReference(drag);
    }

    /**
     * @return the currently dragged data
     */
    public static Object getDrag() {
        return dragging.get();
    }
}
